using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Jasongozy.Copor {
    public class FunctionManager {
        private static Dictionary<string, IFunction> _Functions = new Dictionary<string, IFunction>();


        static FunctionManager() {
            InitFunctions();
        }

        private static void InitFunctions() {
            Type funcType = typeof(AbstractFunction);
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) {
                foreach (Type t in asm.GetTypes()) {
                    if (!t.IsAbstract && t.BaseType == funcType) {
                        IFunction o = (IFunction)Activator.CreateInstance(t);
                        _Functions[o.FuncName] = o;
                    }
                }
            }
        }

        public static string Call(FunctionCallArg arg) {
            if (!_Functions.ContainsKey(arg.FunctionName)) {
                throw new ArgumentException("the function is not defined!");
            }
            IFunction func = _Functions[arg.FunctionName];
            func.IniteProperty(arg.Property);
            return func.Execute();
        }


        
    }
}
